package com.gurb.android;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.media.MediaScannerConnection;


public class MusicList {

	private File castDir = new File("/sdcard/podcast/");

	private final LinkedHashMap<String,File> castList = new LinkedHashMap<String,File>();

	
	private Activity activity;
	
	public MusicList(Activity activity){
		this.activity = activity;
	}
	
	public ArrayList<String> getList(){
		
		final ScannerClient client = new ScannerClient(castDir);
		final MediaScannerConnection msc = new MediaScannerConnection(activity,client);
		client.setScanner(msc);
		msc.connect();

		final Uri uri = android.provider.MediaStore.Audio.Media.getContentUriForPath(castDir.getPath());
		
		String[] projection = new String[] {
				android.provider.MediaStore.Audio.Media._ID,
				android.provider.MediaStore.Audio.Media.TITLE,
				android.provider.MediaStore.Audio.Media.DATA,
				android.provider.MediaStore.Audio.Media.ARTIST,
				android.provider.MediaStore.Audio.Media.ALBUM,
				android.provider.MediaStore.Audio.Media.DISPLAY_NAME,
				"*"};

		String selection = "mime_type like \"audio%\" ";
		Cursor mCursor = null;
		try {
			mCursor = activity.managedQuery(uri, projection,selection, null, 
					android.provider.MediaStore.Audio.Media.DATE_ADDED);
			mCursor.moveToFirst();
		} catch (Exception ex){
			ex.printStackTrace();
		}
		
		final ArrayList<String>  mp3List = new ArrayList<String> ();
		int col = 0;
		int colData = 0;
		int count =1;
		while(mCursor.moveToNext()) {
			dump(mCursor);
			col = getCol(mCursor,new String[]{"PATH","DISPLAY_NAME","NAME","TITLE","ALBUM"});
			colData = getCol(mCursor,new String[]{"_DATA"});
			if (col > -1){
				try {
					if (colData == -1
					|| mCursor.getString(colData).trim().toLowerCase().startsWith(castDir.getAbsolutePath())){
						mp3List.add(count+". "+mCursor.getString(col)); //Adds the artist to the list
						castList.put(count+". "+mCursor.getString(col),new File(mCursor.getString(colData)));
						count++;
					}
				} catch (Exception ex){
					ex.printStackTrace();
				}
			}	
		}		
		
		return mp3List;
	}
	
	private int getCol(Cursor mCursor,String[] values){
		int col = -1;
		for(String c:values){
			col = getCol(mCursor,c);
			if (col > -1){
				return col;
			}
		}
		return -1;
	}
	
	private int getCol(Cursor mCursor, String c){
		try {
			 return mCursor.getColumnIndexOrThrow(c);
		} catch (Exception ex){
			return -1;
		}
	}
	
	public String getFilePath(String d){
		File file = castList.get(d);
		if (isOK(file)) {
			return file.getAbsolutePath();
		}
		return null;
	}
	
	private boolean isOK(File file){
		if (file == null)   return false;
		if (!file.isFile()) return false;
		if (!file.canRead()) return false;
		return (file.getName().trim().toLowerCase().endsWith("mp3") 
			|| file.getName().trim().toLowerCase().endsWith("ogg"));
	}
	
	private void dump(Cursor mCursor){
		// id, title, _data, artist, album, _display_name, _id, _data, _display_name, _size, mime_type, 
		//date_added, date_modified, title, title_key, duration, artist_id, composer, album_id, track, 
		//year, is_ringtone, is_music, is_alarm, is_notification, is_drm, can_forward, can_ringtone, 
		//is_rightsunlimited, is_podcast, bookmark, artist_id:1, artist_key, artist, album_id:1, album_key, album]
		System.err.println("----------- Start -----------");
		try {
			String[] cols = mCursor.getColumnNames();
			String out = null;
			for(String col:cols){
				out = col+" = "+mCursor.getString(mCursor.getColumnIndexOrThrow(col));
				System.err.println(out);
			}
		} catch (Exception ex){
			ex.printStackTrace();
		}
		System.err.println("----------- End -----------");
	
	}

	public String getNext(String c){
		String first = null;
		boolean next = false;
		for(String key:castList.keySet()){
			if (first == null) first = key;
			if (next) return key;
			if (key.endsWith(c)) next = true;
		}
		return first;
	}
	
	public void remove(String key) {
		castList.remove(key);
	}
	
}
